//MINE

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

ENT.iOwner = nil
ENT.HitSound = Sound("ambient/fire/mtov_flame2.wav")

function explodemine(ent,t ,owner)
		if ValidEntity(ent) then
			local vPoint = ent:GetPos() + Vector(0,0,10)
			local effectdata2 = EffectData()
			effectdata2:SetStart( vPoint )
			effectdata2:SetOrigin( vPoint )
			effectdata2:SetScale( 1 )
			util.Effect( "Explosion", effectdata2, true, true )
			local dano = math.random( 5, 30 )
			if t:Health() - dano > 0 then
			t:SetHealth( t:Health() - dano )
			elseif t:Health() - dano <= 0 then
			t.whoKilled = owner
			t:Kill()
			end
			owner:AddFrags(2)
			owner:PrintMessage(HUD_PRINTCENTER, "You hit someone with a mine!")
			t:PrintMessage(HUD_PRINTCENTER,"You got hit by a mine!")
			ent:Remove()
		end	
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "mine_ball" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

function ENT:Think( )
   local nearEnts = ents.FindInSphere( self.Entity:GetPos(), 80 )
   
   for k,v in pairs(nearEnts) do
     if (v:IsPlayer() and v != self.iOwner and v:Team() !=  self.iOwner:Team()) then 
		explodemine(self.Entity, v,self.iOwner)
	 end
   end
end

function ENT:Initialize()

	self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
	self.Entity:DrawShadow( false )
	
	self.Entity:PhysicsInitSphere( 13, "metal_bouncy" )
	self.Entity:SetMoveType( MOVETYPE_NONE ) 
	self.Entity:SetSolid( SOLID_NONE )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	timer.Create("T34"..tostring(math.Rand(0,10000))..tostring(CurTime()/10),120,1, function()
	if ValidEntity(self.Entity) then
	   self.Entity:Remove()
	end
	end)
end

function ENT:PhysicsCollide( data, physobj )
//nothing	
end

function ENT:OnTakeDamage( dmginfo )
//nothing	
end


function ENT:Touch( entidadeTocada )

	if !ValidEntity(entidadeTocada) then return end
	if !entidadeTocada:IsPlayer() then return end
	if entidadeTocada == self.Entity.iOwner or entidadeTocada:Team() == self.Entity.iOwner:Team() then return end

	    if ValidEntity(self.Entity) then
		local vPoint = self.Entity:GetPos() + Vector(0,0,1)
		local effectdata = EffectData()
		effectdata:SetStart( vPoint )
		effectdata:SetOrigin( vPoint )
		effectdata:SetScale( 0.5 )
		util.Effect( "Explosion", effectdata, true, true )
		local dano = math.random( 10, 40 )
		if v:Health() - dano > 0 then
		v:SetHealth( v:Health() - dano )
		elseif v:Health() - dano <= 0 then
		v.whoKilled = owner
		v:Kill()
		end
		self.Entity:Remove()
    	end	

end



